package com.sz.announce.servlet;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONSerializer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import org.springframework.web.servlet.mvc.Controller;

import com.sz.announce.bmo.IAnnounceBMO;
import com.sz.common.ConstDefine;
import com.sz.common.SZUtility;
import com.sz.courseserial.bmo.ICourseSerialBMO;
import com.sz.ticket.bmo.ITicketBMO;

public class OperateAnnounce extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	protected final Log logger = LogFactory.getLog(getClass());
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String ann_type = request.getParameter("ANNOUNCE_TYPE");
		String op_type = request.getParameter("OPERATE_TYPE");
		
		if(ann_type.compareToIgnoreCase("BUSINESS") == 0) {
			
			if(op_type.compareToIgnoreCase("ADD") == 0) {
				add_announce_business(request, response);
			}
		
			
		}
		if(ann_type.compareToIgnoreCase("SEATING") == 0) {
			if (op_type.compareToIgnoreCase("ADD") == 0)  {
			add_announce_seating(request, response);
			} else if (op_type.compareToIgnoreCase("DEL") == 0) {
				delAnnounceSeating(request, response);
			} else if (op_type.compareToIgnoreCase("UPT") == 0) {
				uptAnnounceSeating(request, response);
			}
		}
			
	}
	
	@SuppressWarnings("unchecked")
	private void add_announce_business(HttpServletRequest request, HttpServletResponse response) 
				throws ServletException, IOException{
		try {
				String path = request.getContextPath();
				String error_path = path + "/page/failure.jsp";
				
				String userid = SZUtility.toString(request.getSession().getAttribute("userid")); //当前登陆用户的区域
				
				if(userid.equals("")) {
					String msg = URLEncoder.encode(ConstDefine.s_tip_no_userid, "UTF-8");
					response.sendRedirect(error_path + "?MESSAGE="+msg);
					return;
				}
				
				String business_id = request.getParameter("business_id");
				String title = request.getParameter("ann_title");
				String state = request.getParameter("ann_state");
				String content = request.getParameter("ann_content");
				String begin_time = request.getParameter("ann_begin_time");
				String end_time = request.getParameter("ann_end_time");
				String type = request.getParameter("ann_type");
				
				Map map = new HashMap();
				map.put("BUSINESS_ID", business_id);
				map.put("TITLE", title);
				map.put("STATE", state);
				map.put("CONTENT", content);
				map.put("BEGIN_TIME", begin_time);
				map.put("END_TIME", end_time);
				map.put("CREATE_MANAGER", userid);
				map.put("TYPE", type);
				
				ServletContext servletContext = request.getSession().getServletContext();        
				ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
				
				IAnnounceBMO bmo = (IAnnounceBMO) context.getBean("AnnounceBMO");
				long r = bmo.addAnnounce_business(map);				
				 
				if(r > 0) {					
					response.sendRedirect(path + "/page/success.jsp");
				} else {
					String msg = URLEncoder.encode("新增坐席公告失败", "UTF-8");
					response.sendRedirect(error_path + "?MESSAGE="+msg);
				}	
			} catch (Exception e) {
				logger.error(e.getMessage());
				throw new ServletException(e.getMessage());		
			}
	}
	
	
	@SuppressWarnings("unchecked")
	private void add_announce_seating(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException{
		try {
			
			
			String path = request.getContextPath();
			String error_path = path + "/page/failure.jsp";
			
			String userid = SZUtility.toString(request.getSession().getAttribute("userid")); //当前登陆用户的区域
			
			if(userid.equals("")) {
				String msg = URLEncoder.encode(ConstDefine.s_tip_no_userid, "UTF-8");
				response.sendRedirect(error_path + "?MESSAGE="+msg);
				return;
			}
			

			String title = request.getParameter("ann_title");
			String state = request.getParameter("ann_state");
			String content = request.getParameter("ann_content");
			String begin_time = request.getParameter("ann_begin_time");
			String end_time = request.getParameter("ann_end_time");				
			
			Map map = new HashMap();
			map.put("TITLE", title);
			map.put("STATE", state);
			map.put("CONTENT", content);
			map.put("BEGIN_TIME", begin_time);
			map.put("END_TIME", end_time);
			map.put("CREATE_MANAGER", userid);
			
			ServletContext servletContext = request.getSession().getServletContext();        
			ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
			
			IAnnounceBMO bmo = (IAnnounceBMO) context.getBean("AnnounceBMO");
			long r = bmo.addAnnounce_seating(map);				
			 
			if(r > 0) {					
				response.sendRedirect(path + "/page/success.jsp");
			} else {
				String msg = URLEncoder.encode("新增商家公告失败", "UTF-8");
				response.sendRedirect(error_path + "?MESSAGE="+msg);
			}	
			
			} catch (Exception e) {
				logger.error(e.getMessage());
				throw new ServletException(e.getMessage());		
			}
	}
	
	private void delAnnounceSeating(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException{
		try {
			
String userid = request.getSession().getAttribute("userid").toString(); //当前登陆用户的区域
			
			String json = request.getParameter("PARAM");
			Map param = (Map)JSONSerializer.toJSON(json);

			ServletContext servletContext = request.getSession().getServletContext();        
			ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
			
			IAnnounceBMO bmo = (IAnnounceBMO) context.getBean("AnnounceBMO");
			String msg = bmo.delAnnounce_seating(param);
		

			response.setCharacterEncoding("utf-8");
			response.getWriter().print("删除成功!");
			//response.sendRedirect(path + "/page/success.jsp");
		} catch (Exception e) {
			logger.error(e.getMessage());
			throw new ServletException(e.getMessage());		
		}
	}
	
	private void uptAnnounceSeating(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException{
			try {
			
				String userid = request.getSession().getAttribute("userid").toString(); //当前登陆用户的区域
				
				String json = request.getParameter("PARAM");
				Map param = (Map)JSONSerializer.toJSON(json);

				ServletContext servletContext = request.getSession().getServletContext();        
				ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
				
				IAnnounceBMO bmo = (IAnnounceBMO) context.getBean("AnnounceBMO");
				String msg = bmo.uptAnnounce_seating(param);
			

				response.setCharacterEncoding("utf-8");
				response.getWriter().print("修改成功!");
				//response.sendRedirect(path + "/page/success.jsp");
				
			} catch (Exception e) {
			logger.error(e.getMessage());
			throw new ServletException(e.getMessage());		
			}
		}
	
	
}
